package NetModel.okhttp.requset;


import java.util.Map;

import okhttp3.Call;
import okhttp3.Headers;

/**
 * Created by Administrator on 2016/10/27.
 */
public abstract class BaseRequest<T extends BaseRequest> {
    protected String url;
    protected Map<String, String> heads;
    protected Map<String, String> params;
    protected Object tag;
    protected Integer connectTimeOut = 0;
    protected Integer readTimeOut = 0;
    protected Integer writeTimeOut = 0;

    public T connectTimeOut(Integer connectTimeOut) {
        this.connectTimeOut = connectTimeOut;
        return (T) this;
    }

    public T readTimeOut(Integer readTimeOut) {
        this.readTimeOut = readTimeOut;
        return (T) this;
    }

    public T writeTimeOut(Integer writeTimeOut) {
        this.writeTimeOut = writeTimeOut;
        return (T) this;
    }

    public T url(String url) {
        this.url = url;
        return (T) this;
    }

    public T heads(Map<String, String> heads) {
        if (this.heads == null) {
            this.heads = heads;
        } else {
            this.heads.putAll(heads);
        }
        return (T) this;
    }

    public T params(Map<String, String> params) {
        if (this.params == null) {
            this.params = params;
        } else {
            this.params.putAll(params);
        }
        return (T) this;
    }


    public T tag(Object tag) {
        this.tag = tag;
        return (T) this;
    }


    public abstract Call build();


    protected Headers addHead(Map<String, String> heads) {
        Headers.Builder headBuilder = new Headers.Builder();
        if (heads != null && !heads.isEmpty()) {
            for (Map.Entry<String, String> entry : heads.entrySet())
                headBuilder.add(entry.getKey(), entry.getValue());
        }
        addPublicHead(headBuilder);
        return headBuilder.build();
    }

    private void addPublicHead(Headers.Builder headBuilder) {

    }


}
